package com.iplant.mes.controller.qms;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.QMSService;
import com.iplant.mes.service.po.qms.QMSPassRate;
import com.iplant.mes.service.po.qms.QMSPassStationRate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;

/**
 * 报工单控制器
 *
 * @author YouWang·Peng
 * 2023-8-30 11:26:33
 */
@Slf4j
@RestController
@RequestMapping("/api/QMSPassRate")
public class QMSPassRateController extends BaseController {


    public QMSPassRateController() {
        super();
    }

    @Autowired
    QMSService mQMSService;


    @GetMapping("/OrderTrends")
    public Object OrderTrends(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));
            int wStatType = StringUtils.parseInt(request.getParameter("StatType"),-1);

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassRate>> wServiceResult = mQMSService.QMS_GetPassRateList( wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/ProductTrends")
    public Object ProductTrends(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));
            int wStatType = StringUtils.parseInt(request.getParameter("StatType"),-1);

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassRate>> wServiceResult = mQMSService.QMS_GetProductPassRateList( wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


    @GetMapping("/OrderStat")
    public Object OrderStat(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassRate>> wServiceResult = mQMSService.QMS_GetPassRateList( wLoginUser, wProductID, wOrderID, wOrderNo,  wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/ProductStat")
    public Object ProductStat(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassRate>> wServiceResult = mQMSService.QMS_GetProductPassRateList( wLoginUser, wProductID, wOrderID, wOrderNo, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }



    @GetMapping("/PartpointTrends")
    public Object PartpointTrends(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));
            int wStatType = StringUtils.parseInt(request.getParameter("StatType"),-1);

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassStationRate>> wServiceResult = mQMSService.QMS_GetPassStationRateList( wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/StationTrends")
    public Object StationTrends(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));
            int wStatType = StringUtils.parseInt(request.getParameter("StatType"),-1);

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassStationRate>> wServiceResult = mQMSService.QMS_GetStationPassStationRateList( wLoginUser, wProductID, wOrderID, wOrderNo, wStatType, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


    @GetMapping("/PartpointStat")
    public Object PartpointStat(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassStationRate>> wServiceResult = mQMSService.QMS_GetPassStationRateList( wLoginUser, wProductID, wOrderID, wOrderNo,  wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/StationStat")
    public Object StationStat(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"),-1);
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"),-1);
            String wOrderNo = StringUtils.parseString(request.getParameter("OrderNo"));

            Calendar wStartDate= StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate= StringUtils.parseCalendar(request.getParameter("EndDate"));


            ServiceResult<List<QMSPassStationRate>> wServiceResult = mQMSService.QMS_GetStationPassStationRateList( wLoginUser, wProductID, wOrderID, wOrderNo, wStartDate, wEndDate);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("OrderTrends",ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

}
